package 
{

 import flash.display.Bitmap;
 import flash.display.Loader;
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.events.MouseEvent;
 import flash.net.FileFilter;
 import flash.net.FileReference;
 import flash.text.TextField;

 public class imgLoader extends Sprite
 {
  public var file:FileReference=new FileReference();
//  private var btn:Button=new Button();
	public var txt:TextField  = new TextField();
  public function imgLoader()
  {
   buildUI();
   txt.addEventListener(MouseEvent.CLICK,chooseHandler);
   file.addEventListener(Event.SELECT,addHandler);
   file.addEventListener(Event.COMPLETE,comHandler);

  }
  private function comHandler(e:Event):void
  {

   trace(e.target.name);

   var loader:Loader=new Loader();
   loader.loadBytes(e.target.data);
   loader.contentLoaderInfo.addEventListener(Event.COMPLETE,ImgHandler);

  }
  private function ImgHandler(e:Event):void
  {

   var bit:Bitmap=new Bitmap();
   bit=Bitmap(e.target.content);
   var sp:Sprite=new Sprite();
   sp.addChild(bit);
   addChild(sp);
   bit.width=150;
   bit.height=150;
   sp.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
   sp.addEventListener(MouseEvent.MOUSE_UP,upHandler);
  }
  private function downHandler(e:MouseEvent):void
  {
   e.target.startDrag();
  }
  private function upHandler(e:MouseEvent):void
  {
   e.target.stopDrag();
  }
  private function chooseHandler(e:MouseEvent):void
  {
   file.browse([new FileFilter("jpg文件","*.jpg")]);

  }
  private function addHandler(e:Event):void
  {

   file.load();
  }
  private function buildUI():void
  {
   txt.text="Choose";
   txt.x=10;
   txt.y=stage.stageHeight-40;
   addChild(txt);

  }
 }

}

